package thread;

public class SyncDemo1 {
    public static void main(String[] args) {
        Shar shar1 = new Shar();
        Thread q = new Thread(shar1::buy);
        Thread l = new Thread(shar1::buy);
        //Shar shar2 = new Shar();
        /*
        Thread q = new Thread("张三"){
            public void run(){
                shar1.buy();
            }

        };
        Thread l = new Thread("李四"){
            public void run(){
                shar1.buy();
            }

        };

         */

        q.start();
        l.start();
    }
}
class Shar{
    public  void buy(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"正在吃饭~~~~");
            Thread.sleep(2000);
            synchronized (this){
            //synchronized ("abc"){
                System.out.println(t.getName()+"正在上厕所~~~~~~~~");
                Thread.sleep(2000);
            }
            System.out.println(t.getName()+"洗手走人");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}